package com.ebelter.nb.model.http.request;


import android.text.TextUtils;

import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.NumUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.ebelter.nb.constants.NbConstants.User.phone;

public class NbJsonUtils {

    private static final String DATA = "data";
    private static final String BIZID = "bizId";
    private static final String FUNID = "funcId";
    private static final String VERSION= "version";
    private static final String AUTHKEY = "authKey";



    //拼凑所有的键值对json信息
    private String getMapRJ(Map<String, Serializable> paramMap) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (paramMap != null) {
            for (String key : paramMap.keySet()) {

                sb.append("\"").append(key).append("\":");

                Serializable value = paramMap.get(key);
                if (value instanceof String) {

                    sb.append("\"").append((String) value).append("\"");

                } else if (value instanceof Integer) {
                    sb.append(value);
                } else if (value instanceof Float) {
                    sb.append(value);
                } else if (value instanceof Long) {
                    sb.append(value);
                }
                sb.append(",");
            }
            sb.deleteCharAt(sb.lastIndexOf(","));
        }

        sb.append("}");

        return sb.toString();
    }

    //拼凑所有的键值对json信息
    private String getMapRJ2(Map<String, Serializable> paramMap) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (paramMap != null) {
            for (String key : paramMap.keySet()) {

                sb.append("\"").append(key).append("\":");

                Serializable value = paramMap.get(key);
                if (value instanceof String) {

                    if ("data".equals(key)){
                        sb.append((String) value);
                    }else {
                        sb.append("\"").append((String) value).append("\"");
                    }


                } else if (value instanceof Integer) {
                    sb.append(value);
                } else if (value instanceof Float) {
                    sb.append(value);
                } else if (value instanceof Long) {
                    sb.append(value);
                }
                sb.append(",");
            }
            sb.deleteCharAt(sb.lastIndexOf(","));
        }

        sb.append("}");

        return sb.toString();
    }


    //获取登录的js
    protected String getLoginJs(String loginName,String psw){
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("loginName", loginName);
        paramMap.put("pwd", psw);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "01");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "05");
        paramMap.put(AUTHKEY, "");
        paramMap.put(DATA, dataJs);
        return getMapRJ2(paramMap);
    }

    //注册的js
    public String getRegisterJs(String phone, String name, String sn, String sex, String height,String weight, int profession, String birthday, String dtype, String pwd,String checkCode) {

        Map<String, Serializable> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(name)){
            paramMap.put("name", name);
        }
        if (!TextUtils.isEmpty(sex)){
            paramMap.put("sex", sex);
        }
        if (!TextUtils.isEmpty(birthday)){
            paramMap.put("birthday", birthday);
        }
        if (!TextUtils.isEmpty(height)){
            paramMap.put("height", height);
        }
        if (!TextUtils.isEmpty(weight)){
            paramMap.put("weight", weight);
        }
        if (profession>=0){
            paramMap.put("profession", profession);
        }
        paramMap.put("pwd", MD5Utils.getMd5(pwd));
        paramMap.put("phone", phone);
        if (!TextUtils.isEmpty(sn)){
            paramMap.put("sn", sn);
        }

        paramMap.put("dtype", dtype);
        paramMap.put("checkCode", checkCode);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(BIZID, "01");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "07");
        paramMap.put(AUTHKEY, "");
        paramMap.put(DATA, dataJs);
        return getMapRJ2(paramMap);


    }

    public String getUploadBpmJs(String authKey,String userType, int identification, int ano, String deviceId, String time, int systolic, int diastolic, int pulse) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("systolic", systolic);
        paramMap.put("diastolic", diastolic);
        paramMap.put("pulse", pulse);
        paramMap.put("identification", identification);
        paramMap.put("deviceId", deviceId);
        paramMap.put("ano", ano);
        paramMap.put("time", time);
        paramMap.put("userType", userType);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "19");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "06");
        paramMap.put(AUTHKEY, authKey);
        paramMap.put(DATA, dataJs);
        return getMapRJ2(paramMap);

    }

    public String getUploadScaleJs(String authKey, String deviceId, String time, float muscle, String time1,
                                   float adiposerate, int visceralfat, float moisture, float bone, float thermal,
                                   float basalMetabolism, float metabolism, float bmi, float impedance,float weight,float protein,int hart,
                                   float skeletalMuscle,int physicalAge,int bodyScore) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("muscle", NumUtils.numBaoLiuWei1(muscle));
        paramMap.put("adiposerate", NumUtils.numBaoLiuWei1(adiposerate));
        paramMap.put("visceralfat", visceralfat);
        paramMap.put("moisture", NumUtils.numBaoLiuWei1(moisture));
        paramMap.put("bone", NumUtils.numBaoLiuWei1(bone));
        paramMap.put("thermal", NumUtils.numBaoLiuWei1(thermal));
        paramMap.put("basalMetabolism", NumUtils.numBaoLiuWei1(basalMetabolism));
        paramMap.put("metabolism", NumUtils.numBaoLiuWei1(metabolism));
        paramMap.put("bmi", NumUtils.numBaoLiuWei1(bmi));
        paramMap.put("impedance", NumUtils.numBaoLiuWei1(impedance));
        paramMap.put("deviceId", deviceId);
        paramMap.put("weight", NumUtils.numBaoLiuWei1(weight));
        paramMap.put("protein", protein);
        paramMap.put("hart", hart);
        paramMap.put("skeletalMuscle", NumUtils.numBaoLiuWei1(skeletalMuscle));
        paramMap.put("physicalAge", physicalAge);
        paramMap.put("bodyScore", bodyScore);
        paramMap.put("time", time);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "19");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "10");
        paramMap.put(AUTHKEY, authKey);
        paramMap.put(DATA, dataJs);
        return getMapRJ2(paramMap);

    }

    public String getBpmTrendDatasJs(String authKey, int dateType, String param) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("dateType", dateType);
        paramMap.put("param", param);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "19");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "07");
        paramMap.put(AUTHKEY, authKey);
        paramMap.put(DATA, dataJs);
        return getMapRJ(paramMap);
    }

    public String getrequestPhoneCodeJs(String phone) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("phone", phone);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "01");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "08");
        paramMap.put(AUTHKEY, "");
        paramMap.put(DATA, dataJs);

        String result = getMapRJ2(paramMap);

        return result;
    }

    public String getUpdateUserInfo(String authKey, String name, int sex, String birthday, int height, float weight, int profession, String dtype) {

        Map<String, Serializable> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(name)){
            paramMap.put("name", name);
        }
        if (sex>=0){
            paramMap.put("sex", sex);
        }
        if (!TextUtils.isEmpty(birthday)){
            paramMap.put("birthday", birthday);
        }
        if (height>0){
            paramMap.put("height", height);
        }
        if (weight>0){
            paramMap.put("weight", weight);
        }
        if (profession>=0){
            paramMap.put("profession", profession);
        }
        paramMap.put("dtype",dtype);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "19");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "11");
        paramMap.put(AUTHKEY, authKey);
        paramMap.put(DATA, dataJs);

        return getMapRJ2(paramMap);
    }

    /**
     * @param dateType  1:年 2:月 3周: 4:天
     * @param param 参数值 年：2018  月2018-01 周 2018-01-01（转开始日期就行，后台自动T+7） 天 2018-01-02
     * @param userType 区分用户1/2
     * @param measureType 1血压 2体成分
     */
    public String getTrendData(String authKey, int dateType, String param, String userType, String measureType) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("dateType", dateType);
        paramMap.put("param", param);
        paramMap.put("userType", userType);
        paramMap.put("measureType", measureType);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "19");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "07");
        paramMap.put(AUTHKEY, authKey);
        paramMap.put(DATA, dataJs);
        return getMapRJ2(paramMap);
    }

    String getBloodPressurePageJS(String authKey, int pageNo, int pageSize,String userType) {
        return getPageJS("19", "05", "12", authKey, pageNo, pageSize,userType);
    }

    String getBodyCompositionPageJS(String authKey, int pageNo, int pageSize,String userType) {
        return getPageJS("19", "05", "13", authKey, pageNo, pageSize,userType);
    }

    private String getPageJS(String bizId, String funcId, String version, String authKey, int pageNo, int pageSize,String userType) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        paramMap.put("userType", userType);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put("bizId", bizId);
        paramMap.put("funcId", funcId);
        paramMap.put("version", version);
        paramMap.put("authKey", authKey);
        paramMap.put("data", dataJs);

        return getMapRJ2(paramMap);
    }

    public String getModifyPswJs(String phone, String psw,String checkCode) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("phone", phone);
        paramMap.put("newPassword", MD5Utils.getMd5(psw));
        paramMap.put("checkCode", checkCode);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(BIZID, "01");
        paramMap.put(FUNID, "05");
        paramMap.put(VERSION, "09");
        paramMap.put(AUTHKEY, "");
        paramMap.put(DATA, dataJs);

        return getMapRJ2(paramMap);
    }
}
